package com.gonsin.gview.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gonsin.gview.common.bean.UserBean;
import com.gonsin.gview.common.po.UserBo;
import com.gonsin.gview.common.po.UserSearchPo;
import com.gonsin.gview.common.utils.StringListConverter;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;

/**
 * 用户数据操作类
 */
@Mapper
public interface UserMapper extends BaseMapper<UserBean> {

    @Select("select user_key,username from gs_user")
    List<Map<String, Object>> getAllAvailableUserList();

    /**
     * 按条件搜索账户
     */
    @Results(id = "UserBean", value = {
            @Result(column = "user_key", property = "userKey"),
            @Result(column = "real_name", property = "realName"),
            @Result(column = "parent_user_key", property = "parentUserKey"),
            @Result(column = "child_num", property = "childNum"),
            @Result(column = "root_user_key", property = "rootUserKey"),
            @Result(column = "group_key", property = "groupKey"),
            @Result(column = "edifice_key", property = "edificeKey"),
            @Result(column = "is_admin", property = "admin"),
            @Result(column = "is_root", property = "root"),
            @Result(column = "is_people", property = "people"),
            @Result(column = "mini_openid", property = "miniOpenid"),
            @Result(column = "origin_user_key", property = "originUserKey"),
            @Result(column = "ding_talk_userid", property = "dingTalkUserid"),
            @Result(column = "qy_user_id", property = "qyUserid"),
            @Result(column = "login_id", property = "loginId"),
            @Result(column = "client_id", property = "clientId"),
            @Result(column = "default_audit", property = "defaultAudit"),
            @Result(column = "job_name", property = "jobName"),
            @Result(column = "image_url", property = "imageUrl"),
            @Result(column = "create_time", property = "createTime"),
//            @Result(column = "key_only", property = "keyOnly"),
            @Result(column = "update_time", property = "updateTime"),
//            @Result(column = "card_num", property = "cardNum"),
            @Result(column = "image_md5", property = "imageMd5"),
            @Result(column = "image_url", property = "imageUrl"),
            @Result(column = "gs_permissions", property = "permissions", typeHandler = StringListConverter.class),
            @Result(column = "free_audit", property = "freeAudit"),
            @Result(column = "out_id", property = "outId"),
    })
    @Select("<script>" +
            "select " +
            "m.* " +
            "from gs_user as m " +
            "left join gs_user_user_and_edifice as ue " +
            "on ue.user_key = m.user_key " +
            "where ue.edifice_key = #{edificeKey} " +
            "and (m.soft is null or m.soft = 0) " +
            "and (m.user_type = 'admin' or m.user_type = 'normal')" +
            "<if test=\"searchPo.userKeys != null  &amp;&amp; searchPo.userKeys.size() &gt; 0\"> " +
            "   and m.user_key in " +
            "   <foreach item=\"item\" index=\"index\" collection=\"searchPo.userKeys\" open=\"(\" separator=\",\" close=\")\"> " +
            "       #{item} " +
            "   </foreach> " +
            "</if> " +
            "<if test=\"searchPo.realName != null  &amp;&amp; searchPo.realName.length() > 0\">" +
            "   and m.real_name like CONCAT('%', #{searchPo.realName}, '%') " +
            "</if>" +
            "<if test=\"searchPo.userType != null  &amp;&amp; searchPo.userType != ''\">" +
            "   and m.user_type = #{searchPo.userType}" +
            "</if>" +
            "group by m.user_key " +
            "<if test=\"!pageable.sort.toString().equals('UNSORTED')\">" +
            "    order by " +
            "    <foreach item=\"item\" index=\"index\" collection=\"pageable.sort.orders\" separator=\",\">" +
            "        m.${item.property} ${item.direction}" +
            "    </foreach>" +
            "</if> " +
            "limit ${pageable.page * pageable.size},${pageable.size} " +
            "</script>")
    List<UserBean> searchUser(@Param("edificeKey") String edificeKey,
                              @Param("searchPo") UserSearchPo searchPo,
                              @Param("pageable") Pageable pageable);

    /**
     * 按条件获取账户数量
     */
    @Select("<script>" +
            "select " +
            "ifnull(count(m.user_key), 0) " +
            "from gs_user as m " +
            "left join gs_user_user_and_edifice as ue " +
            "on ue.user_key = m.user_key " +
            "where ue.edifice_key = #{edificeKey} " +
            "and (m.soft is null or m.soft = 0) " +
            "and (m.user_type = 'admin' or m.user_type = 'normal')" +
            "<if test=\"searchPo.userKeys != null  &amp;&amp; searchPo.userKeys.size() &gt; 0\"> " +
            "   and m.user_key in " +
            "   <foreach item=\"item\" index=\"index\" collection=\"searchPo.userKeys\" open=\"(\" separator=\",\" close=\")\"> " +
            "       #{item} " +
            "   </foreach> " +
            "</if> " +
            "<if test=\"searchPo.userType != null  &amp;&amp; searchPo.userType != ''\">" +
            "   and m.user_type = #{searchPo.userType}" +
            "</if>" +
            "<if test=\"searchPo.realName != null  &amp;&amp; searchPo.realName.length() > 0\">" +
            "   and m.real_name like CONCAT('%', #{searchPo.realName}, '%') " +
            "</if>" +
            "</script>")
    int countUser(@Param("edificeKey") String edificeKey,
                  @Param("searchPo") UserSearchPo searchPo);


    /**
     * 根据用户名查询人员的所有信息
     */
    @Results(
            id = "UserBo",
            value = {
                    @Result(column = "user_key", property = "userKey"),
                    @Result(column = "gs_permissions", property = "permissions", typeHandler = StringListConverter.class),
                    @Result(column = "free_audit", property = "freeAudit"),
                    @Result(column = "real_name", property = "realName"),
                    @Result(column = "parent_user_key", property = "parentUserKey"),
                    @Result(column = "child_num", property = "childNum"),
                    @Result(column = "root_user_key", property = "rootUserKey"),
                    @Result(column = "group_key", property = "groupKey"),
                    @Result(column = "edifice_key", property = "edificeKey"),
                    @Result(column = "is_admin", property = "admin"),
                    @Result(column = "is_root", property = "root"),
                    @Result(column = "is_people", property = "people"),
                    @Result(column = "mini_openid", property = "miniOpenid"),
                    @Result(column = "origin_user_key", property = "originUserKey"),
                    @Result(column = "ding_talk_userid", property = "dingTalkUserid"),
                    @Result(column = "qy_userid", property = "qyUserid"),
                    @Result(column = "login_id", property = "loginId"),
                    @Result(column = "client_id", property = "clientId"),
                    @Result(column = "default_audit", property = "defaultAudit"),
                    @Result(column = "job_name", property = "jobName"),
                    @Result(column = "image_url", property = "imageUrl"),
                    @Result(column = "create_time", property = "createTime"),
                    @Result(column = "update_time", property = "updateTime"),
//            @Result(column = "card_num", property = "cardNum"),
                    @Result(column = "image_md5", property = "imageMd5"),
                    @Result(column = "image_url", property = "imageUrl"),
                    @Result(column = "user_info_name", property = "userInfoName"),
            })
//    @ResultMap("UserBo")
    @Select("select " +
            "u.* " +
            "from gs_user as u " +
            "where u.username = #{username}")
    UserBo findByUsername(@Param("username") String username);


    /**
     * 通过loginId查找
     */
    @ResultMap("UserBo")
    @Select("select " +
            "u.* " +
            "from gs_user as u " +
            "where u.login_id = #{loginId}")
    UserBo findByLoginId(@Param("loginId") String loginId);


    @ResultMap("UserBean")
    @Select("<script>" +
            "select " +
            "m.* " +
            "from gs_user as m " +
            "where m.user_key = #{userKey} " +
            "</script>")
    UserBean getUser(@Param("userKey") String userKey);

    @ResultMap("UserBean")
    @Select("<script>" +
            "select " +
            "m.* " +
            "from gs_user as m " +
            "where m.username = #{userName} " +
            "</script>")
    UserBean getUserByName(@Param("userName") String userName);

    @ResultMap("UserBean")
    @Select("select * from gs_user  where is_admin = 1 and edifice_key = #{edificeKey} ")
    List<UserBean> findAllRendAdmin(@Param("edificeKey") String edificeKey);

    @ResultMap("UserBean")
    @Select("select * from gs_user where edifice_key = #{edificeKey} and user_type = 'admin'")
    UserBean getAdminUser(@Param("edificeKey") String edificeKey);

    @ResultMap("UserBean")
    @Select("select * from gs_user where out_id = #{outId}")
    UserBean findByOutId(@Param("outId") String outId);

    @ResultMap("UserBean")
    @Select("select * from gs_user where real_name = #{userInfoName} and username = #{phone}")
    UserBean findByNameAndPhone(@Param("userInfoName") String userInfoName, @Param("phone") String phone);

    @ResultMap("UserBean")
    @Select("select * from gs_user where username = #{phone}")
    UserBean findByPhone(@Param("phone") String phone);

}
